home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 10.7 KB | 367 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWDbgStr.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #include <stddef.h>
-
- // Need to include first so that FW_DEBUG is properly defined
- #ifndef FWDBGSTR_H
- #include "FWDbgStr.h"
- #endif
-
- #ifdef FW_DEBUG
-
- #ifndef FWPRIMEM_H
- #include "FWPriMem.h"
- #endif
-
- #ifndef FWPRISTR_H
- #include "FWPriStr.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__FILES__)
- #include <Files.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__ERRORS__)
- #include <Errors.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
- #include <TextUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
- #include <Script.h>
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWDebug
- #endif
-
- //========================================================================================
- // CLASS FW_CDebugStream
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::EndLine
- //----------------------------------------------------------------------------------------
-
- FW_FUNC_ATTR FW_CDebugStream& EndLine(FW_CDebugStream &stream)
- {
- #ifdef FW_BUILD_MAC
- return stream.Write((void *) "\n", sizeof(char));
- #endif
- #ifdef FW_BUILD_WIN
- return stream.Write((void *) "\n\r", sizeof(char) * 2);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::FW_CDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream::FW_CDebugStream()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::~FW_CDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream::~FW_CDebugStream()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::WriteChunk
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream &FW_CDebugStream::WriteChunk(const void *data, size_t size)
- {
- Write(data, size);
- return Write((void *) " ", sizeof(char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::WriteBase10Number(long n)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::WriteBase10Number(long n)
- {
- char buf[20];
- char *s = &buf[20];
-
- *--s = 0;
- do
- {
- *--s = (char) (n % 10) + '0';
- n /= 10;
- } while (n != 0);
-
- return WriteChunk((void *) s, FW_PrimitiveStringLength(s));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::WriteBase16Number(long n)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::WriteBase16Number(unsigned long n)
- {
- const char *digits = "0123456789ABCDEF";
- char buf[11] = "0x00000000";
- char *s = &buf[11];
-
- for (; n != 0; n /= 16)
- *--s = digits[(short) (n % 16)];
-
- return WriteChunk((void *) buf, 10);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::operator<<(const signed char *string)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::operator<<(const signed char *string)
- {
- return WriteChunk((void *) string, FW_PrimitiveStringLength((const char *) string));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::operator<<(signed char c)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::operator<<(signed char c)
- {
- return WriteChunk((void *) &c, sizeof(char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::operator<<(long n)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::operator<<(long n)
- {
- if (n < 0)
- {
- Write((void *) "-", sizeof(char));
- n = -n;
- }
- return WriteBase10Number(n);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::operator<<(unsigned long n)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::operator<<(unsigned long n)
- {
- return WriteBase10Number(n);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDebugStream::operator<<(void *p)
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CDebugStream::operator<<(void *p)
- {
- return WriteBase16Number((long) p);
- }
-
-
- //========================================================================================
- // CLASS FW_CBufferDebugStream
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CBufferDebugStream::FW_CBufferDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CBufferDebugStream::FW_CBufferDebugStream(void * buffer, size_t bufferSize) :
- fBuffer(buffer),
- fBufferSize(bufferSize),
- fPosition(0)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBufferDebugStream::~FW_CBufferDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CBufferDebugStream::~FW_CBufferDebugStream()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBufferDebugStream::Write
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream &FW_CBufferDebugStream::Write(const void *data, size_t size)
- {
- if (fPosition >= fBufferSize)
- return *this;
-
- size_t writeAmmount = FW_Minimum(size, (size_t)(fBufferSize - fPosition));
-
- FW_PrimitiveCopyMemory(data, (void*)fBuffer, writeAmmount);
-
- return *this;
- }
-
- //========================================================================================
- // CLASS FW_CNullDebugStream
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CNullDebugStream::FW_CNullDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CNullDebugStream::FW_CNullDebugStream()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CNullDebugStream::~FW_CNullDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CNullDebugStream::~FW_CNullDebugStream()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CNullDebugStream::Write
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream &FW_CNullDebugStream::Write(const void *data, size_t size)
- {
- FW_UNUSED(data);
- FW_UNUSED(size);
- return *this;
- }
-
-
- //========================================================================================
- // CLASS FW_CFileDebugStream
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CFileDebugStream::FW_CFileDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CFileDebugStream::FW_CFileDebugStream(char *fileName)
- {
- #ifdef FW_BUILD_MAC
- FSSpec spec;
-
- OSErr err = FSMakeFSSpec(0, 0, c2pstr(fileName), &spec);
- p2cstr((unsigned char *) fileName); // c2pstr modifies the string so lets put it back the way it was
- if (err == fnfErr)
- {
- err = FSpCreate(&spec, 'MPS ', 'TEXT', langEnglish);
- if (err)
- {
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStream: can't create file.");
- }
- }
- else if (err)
- {
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStream: invalid path.");
- }
-
- err = FSpOpenDF(&spec, fsWrPerm, &fMacFileNumber);
- if (err)
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStream: can't open file.");
-
- err = SetEOF(fMacFileNumber, 0);
- if (err)
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStream: can't reset EOF.");
- #endif
- #ifdef FW_BUILD_WIN
- fWinFileHandle = _lcreat(fileName, 0);
- // Create, or open and truncate a file for read and write
-
- if (fWinFileHandle == HFILE_ERROR)
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStream: can't create/open file.");
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFileDebugStream::~FW_CFileDebugStream
- //----------------------------------------------------------------------------------------
-
- FW_CFileDebugStream::~FW_CFileDebugStream()
- {
- #ifdef FW_BUILD_MAC
- OSErr err = FSClose(fMacFileNumber);
-
- if (err)
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::~FW_CFileDebugStream: can't close.");
- #endif
- #ifdef FW_BUILD_WIN
- HFILE result = _lclose(fWinFileHandle);
-
- if (result == HFILE_ERROR)
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::~FW_CFileDebugStream: can't close.");
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFileDebugStream::Write
- //----------------------------------------------------------------------------------------
-
- FW_CDebugStream & FW_CFileDebugStream::Write(const void *data,
- size_t size)
- {
- #ifdef FW_BUILD_MAC
- long count = size;
-
- OSErr err = FSWrite(fMacFileNumber, &count, (Ptr) data);
- if (err)
- {
- FSClose(fMacFileNumber);
- // Go ahead and try closing the file
-
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStreamWrite: can't write.");
- }
- #endif
- #ifdef FW_BUILD_WIN
- long written = _hwrite(fWinFileHandle, (const char*) data, size);
-
- if (written == -1L)
- {
- _lclose(fWinFileHandle);
- // Go ahead and try closing the file
-
- FW_PRIV_DEBUGGER_STRING(
- "FW_CFileDebugStream::FW_CFileDebugStreamWrite: can't write.");
- }
- #endif
-
- return *this;
- }
-
- #endif // FW_DEBUG
-